<template>
  <el-dropdown trigger="click" @command="handleSetLang">
    <div>
      <svg-icon class-name="language-icon" icon-class="language"/>
    </div>
    <el-dropdown-menu slot="dropdown">
      <el-dropdown-item v-for="item of langOptions" :key="item.value" :disabled="lang===item.value"
                        :command="item.value">
        {{ item.label }}
      </el-dropdown-item>
    </el-dropdown-menu>
  </el-dropdown>
</template>

<script>
import {setup} from '@/utils/locales'

export default {
  data() {
    return {
      langOptions: [
        {label: 'en', value: 'en'},
        {label: 'zh', value: 'zh'},
        {label: 'vn', value: 'vn'}
      ]
    }
  },
  computed: {
    lang() {
      return this.$store.getters.lang
    }
  },
  methods: {
    handleSetLang(lang) {
      // this.$ELEMENT.lang = lang
      this.$i18n.locale = lang
      setup(lang)
      // this.$store.dispatch('app/setLang', lang)
      // location.reload();
      // this.refreshView()
      // this.$message({
      //   message: 'Switch Lang Success',
      //   type: 'success'
      // })
    },
    refreshView() {
      // In order to make the cached page re-rendered
      this.$store.dispatch('tagsView/delAllCachedViews', this.$route)

      const {fullPath} = this.$route

      this.$nextTick(() => {
        this.$router.replace({
          path: '/redirect' + fullPath
        })
      })
    }
  }

}
</script>
